#pragma once
#include "CXcpObjectBase.h"
#include "CXcpObjectThreadSafeAddRefPolicy.h"
#include "ITickableFrameScheduler.h"


class CompositorScheduler;

class UIThreadScheduler :
	public CXcpObjectBase<ITickableFrameScheduler,CXcpObjectThreadSafeAddRefPolicy>
{
private: 
	UIThreadScheduler(struct IXcpDispatcher * pDispatcher, CompositorScheduler * pCompositorScheduler);
	virtual ~UIThreadScheduler(); 
	HRESULT Initialize();
public: 
	static HRESULT Create(struct IXcpDispatcher * pDispatcher, struct ICoreCompositorScheduler * pICompositorScheduler, UIThreadScheduler * * ppUIThreadScheduler);
	virtual HRESULT RequestAdditionalFrame(XUINT32 nextTickInterval);
	virtual HRESULT BeginTick();
	virtual HRESULT EndTick();
	virtual XBOOL IsInTick();
	virtual XBOOL IsHighPriority();
	virtual struct IPALTickableClock * GetClock();
	XUINT32 GetScheduledIntervalInMilliseconds(XDOUBLE currentTickTimeInSeconds);
	HRESULT QueueTick();

	enum UIThreadSchedulerState
	{
		UITSS_Waiting = 0x0,
		UITSS_TickQueued = 0x1,
		UITSS_Ticking = 0x2,
	};


private: 
	struct IXcpDispatcher * m_pDispatcher;
	struct IPALTickableClock * m_pIClock;
	XDOUBLE m_lastTickTimeInSeconds;
	CompositorScheduler * m_pCompositorScheduler;
	enum UIThreadSchedulerState m_currentState;
	XUINT32 m_nextTickIntervalInMilliseconds;
	XBOOL m_isHighPriority;
	struct IPALCriticalSection * m_pNextTickLock;


};